<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="85px"
    >
      <el-form-item label="水泵类型:" prop="kind">
        <el-select
          v-model="queryParams.kind"
          filterable
          placeholder="请选择"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="型号:" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="口径:" prop="calibre">
        <el-input
          v-model="queryParams.calibre"
          placeholder="请输入口径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <br />
      <el-form-item label="流量:" prop="flow">
        <el-input
          v-model="queryParams.flow"
          placeholder="请输入流量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扬程:" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入扬程"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="功率:" prop="power">
        <el-input
          v-model="queryParams.power"
          placeholder="请输入功率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转速:" prop="rev">
        <el-input
          v-model="queryParams.rev"
          placeholder="请输入转速"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="kqAllPumpsList" height="630" stripe>
      <el-table-column label="型号" align="left" prop="type" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.type)"
          />
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column label="流量" align="left" prop="flow" sortable min-width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.flow"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.flow)"
          />
          {{ scope.row.flow }}
        </template>
      </el-table-column>
      <el-table-column label="扬程" align="left" prop="height" sortable min-width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.height"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.height)"
          />
          {{ scope.row.height }}
        </template>
      </el-table-column>
      <el-table-column label="功率" align="left" prop="power" sortable width="80">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.power"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.power)"
          />
          {{ scope.row.power }}
        </template>
      </el-table-column>
      <el-table-column label="口径" align="left" prop="calibre" sortable width="80">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.calibre"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.calibre)"
          />
          {{ scope.row.calibre }}
        </template>
      </el-table-column>
      <el-table-column label="转速" align="left" prop="rev" sortable width="80">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.rev"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.rev)"
          />
          {{ scope.row.rev }}
        </template>
      </el-table-column>
      <el-table-column label="安装图1" align="center" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <el-link
            target="_blank"
            class="size_sml"
            :href="scope.row.url1"
            :v-if="!!scope.row.url1"
          >
            {{ !!scope.row.url1 && scope.row.url1.substring(scope.row.url1.lastIndexOf('/')+1,scope.row.url1.length) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="安装图2" align="center" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <el-link
            target="_blank"
            class="size_sml"
            :href="scope.row.url3"
            :v-if="!!scope.row.url3"
          >
            {{ !!scope.row.url3 && scope.row.url3.substring(scope.row.url3.lastIndexOf('/')+1,scope.row.url3.length) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="性能图1" align="center" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <el-link
            target="_blank"
            class="size_sml"
            :href="scope.row.url2"
            :v-if="!!scope.row.url2"
          >
            {{ !!scope.row.url2 && scope.row.url2.substring(scope.row.url2.lastIndexOf('/')+1,scope.row.url2.length) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="性能图2" align="center" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <el-link
            target="_blank"
            :href="scope.row.url4"
            class="size_sml"
            :v-if="!!scope.row.url4"
          >
            {{ !!scope.row.url4 && scope.row.url4.substring(scope.row.url4.lastIndexOf('/')+1,scope.row.url4.length) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="描述"
        align="left"
        prop="description"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.description"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.description)"
          />
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            :loading="modifyLoading"
            @click="handleModify(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[10, 20, 50, 100, 200, 300, 400, 500]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" append-to-body width="80%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label-width="100px" label="id" prop="id">
              <el-input v-model="form.id" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="100px" label="公司缩写:" prop="compType">
              <el-input
                v-model="form.compType"
                placeholder="请输入公司缩写"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label-width="100px" label="产品类型:" prop="kind">
              <el-input v-model="form.kind" placeholder="输入产品类型" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="100px" label="型号:" prop="type">
              <el-input v-model="form.type" placeholder="型号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label-width="100px" label="流量:" prop="flow">
              <el-input v-model="form.flow" placeholder="输入流量" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label-width="100px" label="扬程:" prop="height">
              <el-input v-model="form.height" placeholder="输入扬程" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label-width="100px" label="转速:" prop="rev">
              <el-input v-model="form.rev" placeholder="输入转速" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label-width="100px" label="功率:" prop="power">
              <el-input v-model="form.power" placeholder="输入功率" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="安装图纸1:" prop="url1">
              <el-input v-model="form.url1" placeholder="输入安装图纸1" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="性能图纸1:" prop="url2">
              <el-input v-model="form.url2" placeholder="输入曲线图纸1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="安装图纸2:" prop="url3">
              <el-input v-model="form.url3" placeholder="输入安装图纸2" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="性能图纸2:" prop="url4">
              <el-input v-model="form.url4" placeholder="输入曲线图纸2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label-width="100px"
              label="详情描述:"
              prop="description"
            >
              <el-input
                v-model="form.description"
                minlength="800"
                type="textarea"
                placeholder="详情描述"
                :rows="8"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getKqPumpsInfosList,
  exportKqPumpsInfos,
  getKqPumpsInfo,
  updateKqPumpsInfo
} from "@/api/work/allKqPumps";
export default {
  name: "Work",
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: "",
      modifyLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      kqAllPumpsList: [],
      options: [
        {
          value: "KQL、KQW",
          label: "KQL、KQW【单级泵】"
        },
        {
          value: "KQWH、KQH",
          label: "KQWH、KQH【化工泵】"
        },
        {
          value: "KQWS",
          label: "KQWS"
        },
        {
          value: "KQDP、KQDQ",
          label: "KQDP、KQDQ【冲压泵】"
        },
        {
          value: "WL",
          label: "WL干式排污泵"
        },
        {
          value: "WQE【小排】",
          label: "WQE【小排】"
        },
        {
          value: "WQEC",
          label: "WQEC【小排】【含电泵保护装置】"
        },
        {
          value: "WQ（大排）",
          label: "WQ（大排）"
        },
        {
          value: "WQ（智慧型排污泵）",
          label: "WQ（智慧型排污泵）"
        },
        {
          value: "WQES",
          label: "WQES【轻型切碎式】"
        },
        {
          value: "WQS",
          label: "WQS【切碎式】"
        },
        {
          value: "BWQ",
          label: "BWQ【全不锈钢】"
        },
        {
          value: "WQB",
          label: "WQB【防爆型】"
        },
        {
          value: "KQSN【Φ150-Φ600】",
          label: "KQSN【Φ150-Φ600】【合肥】"
        },
        {
          value: "KQSN【Φ700-Φ1600】",
          label: "KQSN【Φ700-Φ1600】【上海】"
        },
        {
          value: "ZW",
          label: "ZW自吸泵"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 200
        // compName: undefined,
        // collaborator: undefined
        // city: undefined,
        // district: undefined
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getKqPumpsInfosList(this.queryParams).then((res) => {
        this.kqAllPumpsList = res.data.results;
        this.total = res.data.count;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    CopyFileUrl(values) {
      const input = document.createElement("input");
      document.body.appendChild(input);
      input.setAttribute("value", values);
      input.select();
      if (document.execCommand("copy")) {
        document.execCommand("copy");
        this.msgSuccess("复制成功");
      }
      document.body.removeChild(input);
    },
    handleModify(row) {
      this.modifyLoading = true;
      this.reset();
      const id = row.id;
      getKqPumpsInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
        this.modifyLoading = false;
        // if (this.form.classify) {
        //   this.classifyList = this.form.classify.split(",");
        // }
      });
    },
    dealWithUrl() {
      // !!this.form.url1 && this.subStrFunc(url1)
    },
    reset() {
      this.form = {
        id: undefined,
        calibre: undefined,
        description: undefined,
        flow: undefined,
        height: undefined,
        kind: undefined,
        power: undefined,
        rev: undefined
      };
      // this.classifyList = [];
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    submitForm: function() {
      // this.form.classify = this.classifyList.length
      //   ? this.classifyList.toString()
      //   : "";
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateKqPumpsInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有岗位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportKqPumpsInfos(queryParams);
        })
        .then((response) => {
          this.download(response.data.file_url, response.data.name);
        });
    }
  }
};
</script>
<style scoped lang="scss">
.size_sml{
  font-size: 5px;
}
</style>
